﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

// Reviewed
#pragma once
#include "GcmpModelInterface.h"
#include "ElementEnums.h"
#include "DebugMessage.h"
#include "GcmpApiDeclaration.h"
#include "WeakReferenceable.h"

namespace gcmp
{
    class IDocument;
    class IParameterDefinition;
    class UniIdentity;

    /// \brief 参数定义库
    class GCMP_MODEL_INTERFACE_EXPORT IParameterDefinitionLibrary : public WeakReferenceable
    {
        DEFINE_CAST_BASE(gcmp::IParameterDefinitionLibrary)
    public:
        /// \brief 析构函数
        virtual ~IParameterDefinitionLibrary() {}

        /// \brief 获取当前文档的参数定义库
        ///
        /// \param pDocument 文档指针
        /// \param pResult 获取结果
        /// \return 参数定义库
        static IParameterDefinitionLibrary* Get(const IDocument* pDocument, GetElementResult * pResult = nullptr);

        /// \brief 获取给定标识符的参数定义
        ///
        /// \param uid 标识符
        /// \return 参数定义
        virtual const IParameterDefinition* GetParameterDefinitionByUid(const UniIdentity& uid) const = 0;

        /// \brief 添加参数定义
        ///
        /// \param opParameterDefinition 参数定义
        /// \return 成功返回true，失败返回false
        virtual bool AddParameterDefinition(OwnerPtr<IParameterDefinition> opParameterDefinition) = 0;

        /// \brief 更新参数定义
        ///
        /// \param opParameterDefinition 参数定义
        /// \return 成功返回true，失败返回false
        virtual bool UpdateParameterDefinition(OwnerPtr<IParameterDefinition> opParameterDefinition) = 0;

        /// \brief 更新参数定义
        ///
        /// \param pParameterDefinition 参数定义
        /// \return 成功返回true，失败返回false
        virtual bool UpdateParameterDefinition(const IParameterDefinition* pParameterDefinition) = 0;

        /// \brief 根据给定参数在文档中的Id，返回参数定义的跨文档标识符
        ///
        /// \param parameterDefinitionId 参数定义ID
        /// \return 跨文档标识符
        virtual UniIdentity GetParameterDefinitionUidById(int parameterDefinitionId) const = 0;

        /// \brief 根据给定参数定义的跨文档标识符，返回参数定义在文档中的Id
        ///
        /// \param uid 参数定义跨文档标识符
        /// \return 参数定义在文档中的Id
        virtual int GetParameterDefinitionIdByUid(const UniIdentity& uid) const = 0;

        /// \brief 根据标准编码获取对应的参数定义在文档中的Id
        ///
        /// \param standardCode 标准编码
        /// \return 参数定义在文档中的Id。如果失败，则返回-1。
        virtual int GetParameterDefinitionIdByStandardCode(const std::wstring& standardCode) const = 0;
    };
}

/// \brief 获取给定参数名字对应的参数
///
/// \param BUILTIN_PARAM_NAME   参数名字
/// \return 参数
#define PARAMETER(BUILTIN_PARAM_NAME) \
    GetElementParameters()->GetParameterById(PARAMETER_ID(BUILTIN_PARAM_NAME))

/// \brief 获取给定文档元素的参数名字是BUILTIN_PARAM_NAME的参数
///
/// \param ELEMENT             文档元素
/// \param BUILTIN_PARAM_NAME   参数名字
/// \return 参数
#define ELEMENT_PARAMETER(ELEMENT, BUILTIN_PARAM_NAME) \
    ELEMENT->GetElementParameters()->GetParameterById(ELEMENT_PARAMETER_ID(ELEMENT, BUILTIN_PARAM_NAME))

/// \brief 获取给定参数名字对应的参数ID
///
/// \param BUILTIN_PARAM_NAME   参数名字
/// \return 参数ID
#define PARAMETER_ID(BUILTIN_PARAM_NAME) \
    (gcmp::IParameterDefinitionLibrary::Get(GetDocument()) ? gcmp::IParameterDefinitionLibrary::Get(GetDocument())->GetParameterDefinitionIdByUid(PARAMETER_UID(BUILTIN_PARAM_NAME)) : -1)

/// \brief 获取给定文档元素的参数名字是BUILTIN_PARAM_NAME的参数ID
///
/// \param ELEMENT             文档元素
/// \param BUILTIN_PARAM_NAME   参数名字
/// \return 参数ID
#define ELEMENT_PARAMETER_ID(ELEMENT, BUILTIN_PARAM_NAME) \
    gcmp::IParameterDefinitionLibrary::Get(ELEMENT->GetDocument())->GetParameterDefinitionIdByUid(PARAMETER_UID(BUILTIN_PARAM_NAME))

/// \brief 获取参数的跨文档唯一标识符
///
/// \param BUILTIN_PARAM_NAME   参数名字
/// \return 参数的跨文档唯一标识符
#define PARAMETER_UID(BUILTIN_PARAM_NAME) \
    PARAMETER_DEF(BUILTIN_PARAM_NAME)->GetUid()


/// \brief 获取参数定义数据
///
/// \param BUILTIN_PARAM_NAME   参数名字
/// \return 参数定义数据
#define PARAMETER_DEF(BUILTIN_PARAM_NAME) \
    GetParamDef_##BUILTIN_PARAM_NAME()
//gcmp::BuiltInParameterDefinitions::GetParamDef_##BUILTIN_PARAM_NAME()

